*****************************************************************************
* 																			*
*			Campaign Panel Book Do-File Chapter 3 -	Intraindividual Dynamic of Independent Variables for Turnout		*		
*																			*
*****************************************************************************

* The dynamics analyses proceed according to the same scheme as shown in do-file "dynamic reception" and
* will therefore not be expained again.

*---------------------------------------------------------------
* Creation of main diagonal for successive wave pairs and combined for all waves
*---------------------------------------------------------------



foreach num1 of numlist 1/7  {
foreach num2 of numlist 1/7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_entfremda`num1'  {		
		gen a_entfremda`num1'`num2'_hd=.
		replace a_entfremda`num1'`num2'_hd=1 if a_entfremda`num1'==a_entfremda`num2' & a_entfremda`num1'<. & a_entfremda`num2'<.
		replace a_entfremda`num1'`num2'_hd=0 if a_entfremda`num1'!=a_entfremda`num2' & a_entfremda`num1'<. & a_entfremda`num2'<.
		lab var a_entfremda`num1'`num2'_hd "Hauptdiagonale: entfremda 2009"
		tab a_entfremda`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_entfremda`num1'  {		
		gen b_entfremda`num1'`num2'_hd=.
		replace b_entfremda`num1'`num2'_hd=1 if b_entfremda`num1'==b_entfremda`num2' & b_entfremda`num1'<. & b_entfremda`num2'<.
		replace b_entfremda`num1'`num2'_hd=0 if b_entfremda`num1'!=b_entfremda`num2' & b_entfremda`num1'<. & b_entfremda`num2'<.
		lab var b_entfremda`num1'`num2'_hd "Hauptdiagonale: entfremda 2013"
		tab b_entfremda`num1'`num2'_hd [aweight=b_weight], m			
			}
		foreach var1 of varlist a_indiff`num1'  {		
		gen a_indiff`num1'`num2'_hd=.
		replace a_indiff`num1'`num2'_hd=1 if a_indiff`num1'==a_indiff`num2' & a_indiff`num1'<. & a_indiff`num2'<.
		replace a_indiff`num1'`num2'_hd=0 if a_indiff`num1'!=a_indiff`num2' & a_indiff`num1'<. & a_indiff`num2'<.
		lab var a_indiff`num1'`num2'_hd "Hauptdiagonale: indiff 2009"
		tab a_indiff`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_indiff`num1'  {		
		gen b_indiff`num1'`num2'_hd=.
		replace b_indiff`num1'`num2'_hd=1 if b_indiff`num1'==b_indiff`num2' & b_indiff`num1'<. & b_indiff`num2'<.
		replace b_indiff`num1'`num2'_hd=0 if b_indiff`num1'!=b_indiff`num2' & b_indiff`num1'<. & b_indiff`num2'<.
		lab var b_indiff`num1'`num2'_hd "Hauptdiagonale: indiff 2013"
		tab b_indiff`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}

	

foreach num1 of numlist 2/6 {
foreach num2 of numlist 2/6  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_wkampfint`num1'  {		
		gen a_wkampfint`num1'`num2'_hd=.
		replace a_wkampfint`num1'`num2'_hd=1 if a_wkampfint`num1'==a_wkampfint`num2' & a_wkampfint`num1'<. & a_wkampfint`num2'<.
		replace a_wkampfint`num1'`num2'_hd=0 if a_wkampfint`num1'!=a_wkampfint`num2' & a_wkampfint`num1'<. & a_wkampfint`num2'<.
		lab var a_wkampfint`num1'`num2'_hd "Hauptdiagonale: wkampfint 2009"
		tab a_wkampfint`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_wkampfint`num1'  {		
		gen b_wkampfint`num1'`num2'_hd=.
		replace b_wkampfint`num1'`num2'_hd=1 if b_wkampfint`num1'==b_wkampfint`num2' & b_wkampfint`num1'<. & b_wkampfint`num2'<.
		replace b_wkampfint`num1'`num2'_hd=0 if b_wkampfint`num1'!=b_wkampfint`num2' & b_wkampfint`num1'<. & b_wkampfint`num2'<.
		lab var b_wkampfint`num1'`num2'_hd "Hauptdiagonale: wkampfint 2013"
		tab b_wkampfint`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	
	
	
	
foreach num1 of numlist 1 4 6 7 {
foreach num2 of numlist 1 4 6 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_extefficacy`num1'  {		
		gen a_extefficacy`num1'`num2'_hd=.
		replace a_extefficacy`num1'`num2'_hd=1 if a_extefficacy`num1'==a_extefficacy`num2' & a_extefficacy`num1'<. & a_extefficacy`num2'<.
		replace a_extefficacy`num1'`num2'_hd=0 if a_extefficacy`num1'!=a_extefficacy`num2' & a_extefficacy`num1'<. & a_extefficacy`num2'<.
		lab var a_extefficacy`num1'`num2'_hd "Hauptdiagonale: external efficacy 2009"
		tab a_extefficacy`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist a_intefficacy`num1'  {		
		gen a_intefficacy`num1'`num2'_hd=.
		replace a_intefficacy`num1'`num2'_hd=1 if a_intefficacy`num1'==a_intefficacy`num2' & a_intefficacy`num1'<. & a_intefficacy`num2'<.
		replace a_intefficacy`num1'`num2'_hd=0 if a_intefficacy`num1'!=a_intefficacy`num2' & a_intefficacy`num1'<. & a_intefficacy`num2'<.
		lab var a_intefficacy`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2009"
		tab a_intefficacy`num1'`num2'_hd [aweight=a_weight], m			
			}
		}
	}
	}
	
	
foreach num1 of numlist 1 3 7 {
foreach num2 of numlist 1 3 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_extefficacy`num1'  {		
		gen b_extefficacy`num1'`num2'_hd=.
		replace b_extefficacy`num1'`num2'_hd=1 if b_extefficacy`num1'==b_extefficacy`num2' & b_extefficacy`num1'<. & b_extefficacy`num2'<.
		replace b_extefficacy`num1'`num2'_hd=0 if b_extefficacy`num1'!=b_extefficacy`num2' & b_extefficacy`num1'<. & b_extefficacy`num2'<.
		lab var b_extefficacy`num1'`num2'_hd "Hauptdiagonale: external efficacy 2013"
		tab b_extefficacy`num1'`num2'_hd [aweight=b_weight], m				
				}
		foreach var1 of varlist b_intefficacy`num1'  {		
		gen b_intefficacy`num1'`num2'_hd=.
		replace b_intefficacy`num1'`num2'_hd=1 if b_intefficacy`num1'==b_intefficacy`num2' & b_intefficacy`num1'<. & b_intefficacy`num2'<.
		replace b_intefficacy`num1'`num2'_hd=0 if b_intefficacy`num1'!=b_intefficacy`num2' & b_intefficacy`num1'<. & b_intefficacy`num2'<.
		lab var b_intefficacy`num1'`num2'_hd "Hauptdiagonale: internal efficacy 2013"
		tab b_intefficacy`num1'`num2'_hd [aweight=b_weight], m			
			}
		}
	}
	}
	
	
foreach num1 of numlist 1 3 5 7 {
foreach num2 of numlist 1 3 5 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_zufr`num1'  {		
		gen a_zufr`num1'`num2'_hd=.
		replace a_zufr`num1'`num2'_hd=1 if a_zufr`num1'==a_zufr`num2' & a_zufr`num1'<. & a_zufr`num2'<.
		replace a_zufr`num1'`num2'_hd=0 if a_zufr`num1'!=a_zufr`num2' & a_zufr`num1'<. & a_zufr`num2'<.
		lab var a_zufr`num1'`num2'_hd "Hauptdiagonale: satisfaction democracy 2009"
		tab a_zufr`num1'`num2'_hd [aweight=a_weight], m				
				}
		}
	}
	}

	
foreach num1 of numlist 1 7 {
foreach num2 of numlist 1 7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist b_zufr`num1'  {		
		gen b_zufr`num1'`num2'_hd=.
		replace b_zufr`num1'`num2'_hd=1 if b_zufr`num1'==b_zufr`num2' & b_zufr`num1'<. & b_zufr`num2'<.
		replace b_zufr`num1'`num2'_hd=0 if b_zufr`num1'!=b_zufr`num2' & b_zufr`num1'<. & b_zufr`num2'<.
		lab var b_zufr`num1'`num2'_hd "Hauptdiagonale: satisfaction democracy 2013"
		tab b_zufr`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}	
	
****Aggregation on a complete main diagonal for all successive waves

foreach year in a b {
	foreach var in `year'_entfremda `year'_indiff { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/6
	}
}

foreach year in a b {
	foreach var in `year'_wkampfint { 
		gen `var'_hd_ges=(`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd)/4
	}
}


foreach var in a_extefficacy a_intefficacy { 
	gen `var'_hd_ges=(`var'14_hd+`var'46_hd+`var'67_hd)/3
}

foreach var in b_extefficacy b_intefficacy { 
	gen `var'_hd_ges=(`var'13_hd+`var'37_hd)/2
}

foreach var in a_zufr { 
	gen `var'_hd_ges=(`var'13_hd+`var'35_hd+`var'57_hd)/3
}

gen b_zufr_hd_ges = b_zufr17_hd


****Drop single main diagonals


foreach year in a b {
	foreach var in `year'_wkampfint `year'_extefficacy `year'_zufr `year'_entfremda `year'_intefficacy `year'_indiff {
		drop `var'??_hd
	}
}



*We save the data before running the intra-individual regressions
save "${data}\data2009_2013 participants 7 waves.dta", replace


 
*---------------------------------------------------------------------
* Intraindividual Dynamic: Indivdiual Regressions over Time
*----------------------------------------------------------------------

* Create ID-variables for loops

/*Later the loops are supposed to calculate linear regressions of reception mode
over time for each individual respondent looping over all respondents. Since not
all respondents possess valid measures on every reception variable (non-response/don't know)
the loop would stop working if it encountered such a case. The ID-variables are
created for those respondents only who do not have missing values in order to 
guarantee the functioning of the loop.*/



foreach year in a b {
foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}



* Transform data into long format

keep a_wkampfint* a_extefficacy* a_zufr* a_entfremda* a_intefficacy* a_indiff* ///
	b_wkampfint* b_extefficacy* b_zufr* b_entfremda* b_intefficacy* b_indiff* lfdn a_id* b_id*
reshape long a_wkampfint a_extefficacy a_zufr a_entfremda a_intefficacy a_indiff ///
	b_wkampfint b_extefficacy b_zufr b_entfremda b_intefficacy b_indiff, i(lfdn) j(welle)

	
* Individual linear regressions

/* For each individual we want to know whether reception increases, decreases or
remains the same during the campaign. This information shall be saved in `year'_koeff_`var'*/

foreach year in a b {
foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* fill target variable with values
sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	//calculate p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}

										
//Check number of cases
sum b_id_wkampfint
sum b_koeff_wkampfint 	//Correct!

* Save coefficients in new dataset since procedure is very time consuming
reshape wide a_wkampfint a_extefficacy a_zufr a_entfremda a_intefficacy a_indiff ///
	b_wkampfint b_extefficacy b_zufr b_entfremda b_intefficacy b_indiff ///
	a_koeff_wkampfint a_koeff_extefficacy a_koeff_zufr a_koeff_entfremda a_koeff_intefficacy a_koeff_indiff ///
	b_koeff_wkampfint b_koeff_extefficacy b_koeff_zufr b_koeff_entfremda b_koeff_intefficacy b_koeff_indiff ///
	a_rohkoeff_wkampfint a_rohkoeff_extefficacy a_rohkoeff_zufr a_rohkoeff_entfremda a_rohkoeff_intefficacy a_rohkoeff_indiff ///
	b_rohkoeff_wkampfint b_rohkoeff_extefficacy b_rohkoeff_zufr b_rohkoeff_entfremda b_rohkoeff_intefficacy b_rohkoeff_indiff, ///
	i(lfdn) j(welle)

keep lfdn ?_koeff_*1 ?_rohkoeff_*1 ?_koeff_wkampfint2 ?_rohkoeff_wkampfint2	
							// The coefficient variable from one wave alone is 
							//sufficient, since the variable is constant
							//(the coefficient was calculated over all waves)
							
foreach ding in extefficacy zufr entfremda intefficacy indiff {
rename ?_koeff_`ding'1 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'1 ?_rohkoeff_`ding'
}

foreach ding in wkampfint {
rename ?_koeff_`ding'2 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'2 ?_rohkoeff_`ding'
}

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient"
lab val ?_koeff_* koeff

save "${data}\data 2009 2013 IIR Chapter 3 wbt.dta", replace


*-----------------------------------------------------------------
* Table: Stability / Dynamic of Receptionfrequencies
*----------------------------------------------------------------

/* Measure of stability: cross average main diagonal percentages with intraindividual 
regression coefficients

In order to achieve this, first merge regression coefficients to dataset */

use "${data}\data2009_2013 participants 7 waves.dta",clear

//drop _merge
merge 1:1 lfdn using "${data}\data 2009 2013 IIR Chapter 3 wbt.dta"
drop _merge

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient", replace
lab val ?_koeff_* koeff

* Categorize intraindividual raw coefficients (without test of significance)

foreach year in a b {
	foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}


* Final table	

**************************************************
***Tables 15 to 20
**************************************************		


**Interest in the election campaign


***Aggregation of wave means
estimates clear
foreach year in a b {
foreach var in wkampfint {

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6)/5
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/3
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'6)/2
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in a b{
	foreach var in wkampfint {
		foreach num of numlist 2/6 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*4)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a b{
	foreach var in wkampfint {
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'56

}
}


foreach year in a b{
	foreach var in wkampfint {
		foreach num in 23 34 45 56 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a b{
	foreach var in wkampfint {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean)/4
		tab `year'_stab_`var'_aggr
}
}
*

** Separate analyses for political interest and existance of a party identification

** Separate for political interest

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1 2{
foreach var in wkampfint {

sum `year'_`var'2 if `year'_`var'_hd_ges~=. & `year'_polinttri1==`num'		//static mean, w2
gen `year'_mean`var'_pol`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w3
gen `year'_mean`var'_pol`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w4
gen `year'_mean`var'_pol`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w5
gen `year'_mean`var'_pol`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w6
gen `year'_mean`var'_pol`num'6 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4+`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6)/5
tab1 `year'_sum`var'_pol`num'
gen `year'_sum24`var'_pol`num' = (`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4)/3
tab1 `year'_sum24`var'_pol`num'
gen `year'_sum56`var'_pol`num' = (`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6)/2
tab1 `year'_sum56`var'_pol`num'

}
}
}
*Differences

foreach year in a b{
foreach num in 0 1 2{
	foreach var in wkampfint {
gen `year'_diff_`var'_pol`num'23=`year'_`var'3n-`year'_`var'2n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'23

gen `year'_diff_`var'_pol`num'34=`year'_`var'4n-`year'_`var'3n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'34

gen `year'_diff_`var'_pol`num'45=`year'_`var'5n-`year'_`var'4n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'45

gen `year'_diff_`var'_pol`num'56=`year'_`var'6n-`year'_`var'5n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'56

gen `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'_pol`num'23

gen `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'_pol`num'34

gen `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'_pol`num'45

gen `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'_pol`num'56

}
}
}

foreach year in a b{
foreach num in 0 1 2{
	foreach var in wkampfint {
		foreach num2 in 23 34 45 56 {
			ci means `year'_stab_`var'_pol`num'`num2'
			gen `year'_stab_`var'_pol`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1 2{
	foreach var in wkampfint {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'23_mean+`year'_stab_`var'_pol`num'34_mean+`year'_stab_`var'_pol`num'45_mean+`year'_stab_`var'_pol`num'56_mean)/4
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}
*




**Separate for party identification
***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1{
foreach var in wkampfint {

sum `year'_`var'2 if `year'_`var'_hd_ges~=. & `year'_pidyes1==`num'		//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6)/5
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/3
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6)/2
tab1 `year'_sum56`var'_pid`num'

}
}
}
*Differences

foreach year in a b{
foreach num in 0 1{
	foreach var in wkampfint {
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'_pid`num'56

}
}
}

foreach year in a b{
foreach num in 0 1{
	foreach var in wkampfint {
		foreach num2 in 23 34 45 56 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1{
	foreach var in wkampfint {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean)/4
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*









**External and Internal Efficacy


***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'4+`year'_mean`var'6+`year'_mean`var'7)/4
tab1 `year'_sum`var'
gen `year'_sum14`var' = (`year'_mean`var'1+`year'_mean`var'4)/2
tab1 `year'_sum14`var'
gen `year'_sum67`var' = (`year'_mean`var'6+`year'_mean`var'7)/2
tab1 `year'_sum67`var'

}
}


estimates clear
foreach year in b {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'3+`year'_mean`var'7)/3
tab1 `year'_sum`var'
gen `year'_sum13`var' = (`year'_mean`var'1+`year'_mean`var'3)/2
tab1 `year'_sum13`var'
gen `year'_sum57`var' = `year'_mean`var'7
tab1 `year'_sum57`var'

}
}

*Aggregate stability preparation


foreach year in a{
	foreach var in extefficacy intefficacy {
		foreach num of numlist 1 4 6 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*8)
	tab `year'_`var'`num'n
}
}
}
*
foreach year in b{
	foreach var in extefficacy intefficacy {
		foreach num of numlist 1 3 7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*8)
	tab `year'_`var'`num'n
}
}
}
*
*Differences
*2009
foreach year in a{
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'14=`year'_`var'4n-`year'_`var'1n
tab `year'_diff_`var'14

gen `year'_diff_`var'46=`year'_`var'6n-`year'_`var'4n
tab `year'_diff_`var'46

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'14 =1-abs(`year'_diff_`var'14/4) if `year'_`var'1n==4
tab `year'_stab_`var'14

gen `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/8) if `year'_`var'4n==0 | `year'_`var'4n==8
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/7) if `year'_`var'4n==1 | `year'_`var'4n==7
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/6) if `year'_`var'4n==2 | `year'_`var'4n==6
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/5) if `year'_`var'4n==3 | `year'_`var'4n==5
replace `year'_stab_`var'46 =1-abs(`year'_diff_`var'46/4) if `year'_`var'4n==4
tab `year'_stab_`var'46

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==0 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==1 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==2 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==3 | `year'_`var'6n==5
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/4) if `year'_`var'6n==4
tab `year'_stab_`var'67

}
}


foreach year in a{
	foreach var in extefficacy intefficacy {
		foreach num in 14 46 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'14_mean+`year'_stab_`var'46_mean+`year'_stab_`var'67_mean)/3
		tab `year'_stab_`var'_aggr
}
}




*2013
foreach year in b{
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'13=`year'_`var'3n-`year'_`var'1n
tab `year'_diff_`var'13

gen `year'_diff_`var'37=`year'_`var'7n-`year'_`var'3n
tab `year'_diff_`var'37

gen `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'13 =1-abs(`year'_diff_`var'13/4) if `year'_`var'1n==4
tab `year'_stab_`var'13

gen `year'_stab_`var'37 =1-abs(`year'_diff_`var'37/8) if `year'_`var'3n==0 | `year'_`var'3n==8
replace `year'_stab_`var'37 =1-abs(`year'_diff_`var'37/7) if `year'_`var'3n==1 | `year'_`var'3n==7
replace `year'_stab_`var'37 =1-abs(`year'_diff_`var'37/6) if `year'_`var'3n==2 | `year'_`var'3n==6
replace `year'_stab_`var'37 =1-abs(`year'_diff_`var'37/5) if `year'_`var'3n==3 | `year'_`var'3n==5
replace `year'_stab_`var'37 =1-abs(`year'_diff_`var'37/4) if `year'_`var'3n==4
tab `year'_stab_`var'37

}
}


foreach year in b{
	foreach var in extefficacy intefficacy {
		foreach num in 13 37 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in b{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'13_mean+`year'_stab_`var'37_mean)/2
		tab `year'_stab_`var'_aggr
}
}




** Separate analyses for political interest and existance of a party identification

**Separate for political interest



***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 0 1 2 {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'		//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'		//static mean, w4
gen `year'_mean`var'_pol`num'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'		//static mean, w6
gen `year'_mean`var'_pol`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'		//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'4+`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/4
tab1 `year'_sum`var'_pol`num'
gen `year'_sum14`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'4)/2
tab1 `year'_sum14`var'_pol`num'
gen `year'_sum67`var'_pol`num' = (`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/2
tab1 `year'_sum67`var'_pol`num'

}
}
}


estimates clear
foreach year in b {
foreach num in 0 1 2 {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w3
gen `year'_mean`var'_pol`num'3 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'7)/3
tab1 `year'_sum`var'_pol`num'
gen `year'_sum13`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'3)/2
tab1 `year'_sum13`var'_pol`num'
gen `year'_sum57`var'_pol`num' = `year'_mean`var'_pol`num'7
tab1 `year'_sum57`var'_pol`num'

}
}
}

*Differences
*2009
foreach year in a{
foreach num in 0 1 2 {
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'_pol`num'14=`year'_`var'4n-`year'_`var'1n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'14

gen `year'_diff_`var'_pol`num'46=`year'_`var'6n-`year'_`var'4n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'46

gen `year'_diff_`var'_pol`num'67=`year'_`var'7n-`year'_`var'6n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'67

gen `year'_stab_`var'_pol`num'14 =1-abs(`year'_diff_`var'_pol`num'14/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'_pol`num'14 =1-abs(`year'_diff_`var'_pol`num'14/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'_pol`num'14 =1-abs(`year'_diff_`var'_pol`num'14/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'_pol`num'14 =1-abs(`year'_diff_`var'_pol`num'14/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'_pol`num'14 =1-abs(`year'_diff_`var'_pol`num'14/4) if `year'_`var'1n==4
tab `year'_stab_`var'_pol`num'14

gen `year'_stab_`var'_pol`num'46 =1-abs(`year'_diff_`var'_pol`num'46/8) if `year'_`var'4n==0 | `year'_`var'4n==8
replace `year'_stab_`var'_pol`num'46 =1-abs(`year'_diff_`var'_pol`num'46/7) if `year'_`var'4n==1 | `year'_`var'4n==7
replace `year'_stab_`var'_pol`num'46 =1-abs(`year'_diff_`var'_pol`num'46/6) if `year'_`var'4n==2 | `year'_`var'4n==6
replace `year'_stab_`var'_pol`num'46 =1-abs(`year'_diff_`var'_pol`num'46/5) if `year'_`var'4n==3 | `year'_`var'4n==5
replace `year'_stab_`var'_pol`num'46 =1-abs(`year'_diff_`var'_pol`num'46/4) if `year'_`var'4n==4
tab `year'_stab_`var'_pol`num'46

gen `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/8) if `year'_`var'6n==0 | `year'_`var'6n==8
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/7) if `year'_`var'6n==1 | `year'_`var'6n==7
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/6) if `year'_`var'6n==2 | `year'_`var'6n==6
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/5) if `year'_`var'6n==3 | `year'_`var'6n==5
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/4) if `year'_`var'6n==4
tab `year'_stab_`var'_pol`num'67

}
}
}

foreach year in a{
foreach num in 0 1 2{
	foreach var in extefficacy intefficacy {
		foreach num2 in 14 46 67 {
			ci means `year'_stab_`var'_pol`num'`num2'
			gen `year'_stab_`var'_pol`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a{
foreach num in 0 1 2{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'14_mean+`year'_stab_`var'_pol`num'46_mean+`year'_stab_`var'_pol`num'67_mean)/3
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}




*2013
foreach year in b{
foreach num in 0 1 2{
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'_pol`num'13=`year'_`var'3n-`year'_`var'1n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'13

gen `year'_diff_`var'_pol`num'37=`year'_`var'7n-`year'_`var'3n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'37

gen `year'_stab_`var'_pol`num'13 =1-abs(`year'_diff_`var'_pol`num'13/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'_pol`num'13 =1-abs(`year'_diff_`var'_pol`num'13/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'_pol`num'13 =1-abs(`year'_diff_`var'_pol`num'13/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'_pol`num'13 =1-abs(`year'_diff_`var'_pol`num'13/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'_pol`num'13 =1-abs(`year'_diff_`var'_pol`num'13/4) if `year'_`var'1n==4
tab `year'_stab_`var'_pol`num'13

gen `year'_stab_`var'_pol`num'37 =1-abs(`year'_diff_`var'_pol`num'37/8) if `year'_`var'3n==0 | `year'_`var'3n==8
replace `year'_stab_`var'_pol`num'37 =1-abs(`year'_diff_`var'_pol`num'37/7) if `year'_`var'3n==1 | `year'_`var'3n==7
replace `year'_stab_`var'_pol`num'37 =1-abs(`year'_diff_`var'_pol`num'37/6) if `year'_`var'3n==2 | `year'_`var'3n==6
replace `year'_stab_`var'_pol`num'37 =1-abs(`year'_diff_`var'_pol`num'37/5) if `year'_`var'3n==3 | `year'_`var'3n==5
replace `year'_stab_`var'_pol`num'37 =1-abs(`year'_diff_`var'_pol`num'37/4) if `year'_`var'3n==4
tab `year'_stab_`var'_pol`num'37

}
}
}


foreach year in b{
foreach num in 0 1 2{
	foreach var in extefficacy intefficacy {
		foreach num2 in 13 37 {
			ci means `year'_stab_`var'_pol`num'`num2'
			gen `year'_stab_`var'_pol`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 0 1 2{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'13_mean+`year'_stab_`var'_pol`num'37_mean)/2
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}







**Separate analysis for party identification


***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 0 1 {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'		//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'		//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'		//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'		//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/4
tab1 `year'_sum`var'_pid`num'
gen `year'_sum14`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'4)/2
tab1 `year'_sum14`var'_pid`num'
gen `year'_sum67`var'_pid`num' = (`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum67`var'_pid`num'

}
}
}


estimates clear
foreach year in b {
foreach num in 0 1 {
foreach var in extefficacy intefficacy {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum`var'_pid`num'
gen `year'_sum13`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3)/2
tab1 `year'_sum13`var'_pid`num'
gen `year'_sum57`var'_pid`num' = `year'_mean`var'_pid`num'7
tab1 `year'_sum57`var'_pid`num'

}
}
}

*Differences
*2009
foreach year in a{
foreach num in 0 1 {
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'_pid`num'14=`year'_`var'4n-`year'_`var'1n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'14

gen `year'_diff_`var'_pid`num'46=`year'_`var'6n-`year'_`var'4n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'46

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'_pid`num'14 =1-abs(`year'_diff_`var'_pid`num'14/4) if `year'_`var'1n==4
tab `year'_stab_`var'_pid`num'14

gen `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/8) if `year'_`var'4n==0 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/7) if `year'_`var'4n==1 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/6) if `year'_`var'4n==2 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/5) if `year'_`var'4n==3 | `year'_`var'4n==5
replace `year'_stab_`var'_pid`num'46 =1-abs(`year'_diff_`var'_pid`num'46/4) if `year'_`var'4n==4
tab `year'_stab_`var'_pid`num'46

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==0 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==1 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==2 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==3 | `year'_`var'6n==5
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/4) if `year'_`var'6n==4
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a{
foreach num in 0 1{
	foreach var in extefficacy intefficacy {
		foreach num2 in 14 46 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a{
foreach num in 0 1{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'14_mean+`year'_stab_`var'_pid`num'46_mean+`year'_stab_`var'_pid`num'67_mean)/3
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}




*2013
foreach year in b{
foreach num in 0 1{
	foreach var in extefficacy intefficacy {
gen `year'_diff_`var'_pid`num'13=`year'_`var'3n-`year'_`var'1n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'13

gen `year'_diff_`var'_pid`num'37=`year'_`var'7n-`year'_`var'3n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'37

gen `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/8) if `year'_`var'1n==0 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/7) if `year'_`var'1n==1 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/6) if `year'_`var'1n==2 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/5) if `year'_`var'1n==3 | `year'_`var'1n==5
replace `year'_stab_`var'_pid`num'13 =1-abs(`year'_diff_`var'_pid`num'13/4) if `year'_`var'1n==4
tab `year'_stab_`var'_pid`num'13

gen `year'_stab_`var'_pid`num'37 =1-abs(`year'_diff_`var'_pid`num'37/8) if `year'_`var'3n==0 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'37 =1-abs(`year'_diff_`var'_pid`num'37/7) if `year'_`var'3n==1 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'37 =1-abs(`year'_diff_`var'_pid`num'37/6) if `year'_`var'3n==2 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'37 =1-abs(`year'_diff_`var'_pid`num'37/5) if `year'_`var'3n==3 | `year'_`var'3n==5
replace `year'_stab_`var'_pid`num'37 =1-abs(`year'_diff_`var'_pid`num'37/4) if `year'_`var'3n==4
tab `year'_stab_`var'_pid`num'37

}
}
}


foreach year in b{
foreach num in 0 1{
	foreach var in extefficacy intefficacy {
		foreach num2 in 13 37 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 0 1{
	foreach var in extefficacy intefficacy {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'13_mean+`year'_stab_`var'_pid`num'37_mean)/2
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}





















**Indifference and Alienation


***Aggregation of wave means
estimates clear
foreach year in a b {
foreach var in entfremda indiff {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum56`var'

}
}

*Aggregate stability preparation


foreach year in a b{
	foreach var in entfremda indiff {
		foreach num of numlist 1/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*10)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a b{
	foreach var in entfremda indiff {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'67

}
}


foreach year in a b{
	foreach var in entfremda indiff {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a b{
	foreach var in entfremda indiff {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}
*



*** Separate analyses for political interest and existance of a party identification
**Separate analysis for political interest

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1 2 {
foreach var in entfremda indiff {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_polinttri1==`num'	//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w2
gen `year'_mean`var'_pol`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w3
gen `year'_mean`var'_pol`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w4
gen `year'_mean`var'_pol`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w5
gen `year'_mean`var'_pol`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w6
gen `year'_mean`var'_pol`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4+`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/7
tab1 `year'_sum`var'_pol`num'
gen `year'_sum24`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4)/4
tab1 `year'_sum24`var'_pol`num'
gen `year'_sum56`var'_pol`num' = (`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/3
tab1 `year'_sum56`var'_pol`num'

}
}
}

*Differences

foreach year in a b{
foreach num in 0 1 2{
	foreach var in entfremda indiff {
gen `year'_diff_`var'_pol`num'12=`year'_`var'2n-`year'_`var'1n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'12
	
gen `year'_diff_`var'_pol`num'23=`year'_`var'3n-`year'_`var'2n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'23

gen `year'_diff_`var'_pol`num'34=`year'_`var'4n-`year'_`var'3n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'34

gen `year'_diff_`var'_pol`num'45=`year'_`var'5n-`year'_`var'4n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'45

gen `year'_diff_`var'_pol`num'56=`year'_`var'6n-`year'_`var'5n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'56

gen `year'_diff_`var'_pol`num'67=`year'_`var'7n-`year'_`var'6n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'67

gen `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pol`num'12

gen `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pol`num'23

gen `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pol`num'34

gen `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pol`num'45

gen `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'_pol`num'56

gen `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pol`num'67

}
}
}

foreach year in a b{
foreach num in 0 1 2 {
	foreach var in entfremda indiff {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pol`num'`num2'
			gen `year'_stab_`var'_pol`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1 2 {
	foreach var in entfremda indiff {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'12_mean+`year'_stab_`var'_pol`num'23_mean+`year'_stab_`var'_pol`num'34_mean+`year'_stab_`var'_pol`num'45_mean+`year'_stab_`var'_pol`num'56_mean+`year'_stab_`var'_pol`num'67_mean)/6
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}
*







*Separate analysis for party identification





***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1 {
foreach var in entfremda indiff {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.  & `year'_pidyes1==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	
	
sum `year'_`var'2 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences

foreach year in a b{
foreach num in 0 1{
	foreach var in entfremda indiff {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'12
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/10) if `year'_`var'1n==0 | `year'_`var'1n==10
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/9) if `year'_`var'1n==1 | `year'_`var'1n==9
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/8) if `year'_`var'1n==2 | `year'_`var'1n==8
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/7) if `year'_`var'1n==3 | `year'_`var'1n==7
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/6) if `year'_`var'1n==4 | `year'_`var'1n==6
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/5) if `year'_`var'1n==5
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/10) if `year'_`var'2n==0 | `year'_`var'2n==10
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/9) if `year'_`var'2n==1 | `year'_`var'2n==9
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/8) if `year'_`var'2n==2 | `year'_`var'2n==8
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/7) if `year'_`var'2n==3 | `year'_`var'2n==7
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/6) if `year'_`var'2n==4 | `year'_`var'2n==6
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/5) if `year'_`var'2n==5
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/10) if `year'_`var'3n==0 | `year'_`var'3n==10
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/9) if `year'_`var'3n==1 | `year'_`var'3n==9
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/8) if `year'_`var'3n==2 | `year'_`var'3n==8
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/7) if `year'_`var'3n==3 | `year'_`var'3n==7
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/6) if `year'_`var'3n==4 | `year'_`var'3n==6
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/5) if `year'_`var'3n==5
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/10) if `year'_`var'4n==0 | `year'_`var'4n==10
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/9) if `year'_`var'4n==1 | `year'_`var'4n==9
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/8) if `year'_`var'4n==2 | `year'_`var'4n==8
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/7) if `year'_`var'4n==3 | `year'_`var'4n==7
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/6) if `year'_`var'4n==4 | `year'_`var'4n==6
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/5) if `year'_`var'4n==5
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/10) if `year'_`var'5n==0 | `year'_`var'5n==10
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/9) if `year'_`var'5n==1 | `year'_`var'5n==9
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/8) if `year'_`var'5n==2 | `year'_`var'5n==8
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/7) if `year'_`var'5n==3 | `year'_`var'5n==7
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/6) if `year'_`var'5n==4 | `year'_`var'5n==6
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/5) if `year'_`var'5n==5
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/10) if `year'_`var'6n==0 | `year'_`var'6n==10
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/9) if `year'_`var'6n==1 | `year'_`var'6n==9
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/8) if `year'_`var'6n==2 | `year'_`var'6n==8
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/7) if `year'_`var'6n==3 | `year'_`var'6n==7
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/6) if `year'_`var'6n==4 | `year'_`var'6n==6
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/5) if `year'_`var'6n==5
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a b{
foreach num in 0 1 {
	foreach var in entfremda indiff {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1 {
	foreach var in entfremda indiff {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean+`year'_stab_`var'_pid`num'67_mean)/6
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*



**Satisfaction with democracy


***Aggregation of wave means
estimates clear
foreach year in a {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'3+`year'_mean`var'5+`year'_mean`var'7)/4
tab1 `year'_sum`var'
gen `year'_sum24`var' = (`year'_mean`var'1+`year'_mean`var'3)/2
tab1 `year'_sum24`var'
gen `year'_sum56`var' = (`year'_mean`var'5+`year'_mean`var'7)/2
tab1 `year'_sum56`var'

}
}



***Aggregation of wave means
estimates clear
foreach year in  b {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)	

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'7)/2
tab1 `year'_sum`var'
gen `year'_sum24`var' = `year'_mean`var'1
tab1 `year'_sum24`var'
gen `year'_sum56`var' = `year'_mean`var'7
tab1 `year'_sum56`var'

}
}


*2009
*Aggregate stability preparation
		foreach num of numlist 1 3 5 7 {
	gen a_zufr`num'n = round(a_zufr`num'*4)
	tab a_zufr`num'n
}
*

*Differences
gen a_diff_zufr13=a_zufr3n-a_zufr1n
tab a_diff_zufr13

gen a_diff_zufr35=a_zufr5n-a_zufr3n
tab a_diff_zufr35

gen a_diff_zufr57=a_zufr7n-a_zufr5n
tab a_diff_zufr57

gen a_stab_zufr13 =1-abs(a_diff_zufr13/4) if a_zufr1n==0 | a_zufr1n==4
replace a_stab_zufr13 =1-abs(a_diff_zufr13/3) if a_zufr1n==1 | a_zufr1n==3
replace a_stab_zufr13 =1-abs(a_diff_zufr13/2) if a_zufr1n==2
tab a_stab_zufr13

gen a_stab_zufr35 =1-abs(a_diff_zufr35/4) if a_zufr3n==0 | a_zufr3n==4
replace a_stab_zufr35 =1-abs(a_diff_zufr35/3) if a_zufr3n==1 | a_zufr3n==3
replace a_stab_zufr35 =1-abs(a_diff_zufr35/2) if a_zufr3n==2
tab a_stab_zufr35

gen a_stab_zufr57 =1-abs(a_diff_zufr57/4) if a_zufr5n==0 | a_zufr5n==4
replace a_stab_zufr57 =1-abs(a_diff_zufr57/3) if a_zufr5n==1 | a_zufr5n==3
replace a_stab_zufr57 =1-abs(a_diff_zufr57/2) if a_zufr5n==2
tab a_stab_zufr57


foreach num in 13 35 57 {
	ci means a_stab_zufr`num'
	gen a_stab_zufr`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in a{
	foreach var in zufr {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'13_mean+`year'_stab_`var'35_mean+`year'_stab_`var'57_mean)/3
		tab `year'_stab_`var'_aggr
}
}


*2013

*Aggregate stability preparation
		foreach num of numlist 1 7 {
	gen b_zufr`num'n = round(b_zufr`num'*4)
	tab b_zufr`num'n
}
*

*Differences
gen b_diff_zufr17=b_zufr7n-b_zufr1n
tab b_diff_zufr17


gen b_stab_zufr17 =1-abs(b_diff_zufr17/4) if b_zufr1n==0 | b_zufr1n==4
replace b_stab_zufr17 =1-abs(b_diff_zufr17/3) if b_zufr1n==1 | b_zufr1n==3
replace b_stab_zufr17 =1-abs(b_diff_zufr17/2) if b_zufr1n==2
tab b_stab_zufr17


foreach num in 17 {
	ci means b_stab_zufr`num'
	gen b_stab_zufr`num'_mean = r(mean)
}

*

*Aggregate stability
foreach year in b{
	foreach var in zufr {
		gen `year'_stab_`var'_aggr=`year'_stab_`var'17_mean
		tab `year'_stab_`var'_aggr
}
}



***Separate for political interest and existance of party identification



*Separate for political interest


***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 0 1 2 {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	& `year'_polinttri==`num'	//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		& `year'_polinttri==`num'	//static mean, w3
gen `year'_mean`var'_pol`num'3 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	& `year'_polinttri==`num'		//static mean, w5
gen `year'_mean`var'_pol`num'5 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		& `year'_polinttri==`num'	//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'7)/4
tab1 `year'_sum`var'_pol`num'
gen `year'_sum24`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'3)/2
tab1 `year'_sum24`var'_pol`num'
gen `year'_sum56`var'_pol`num' = (`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'7)/2
tab1 `year'_sum56`var'_pol`num'

}
}
}



***Aggregation of wave means
estimates clear
foreach year in  b {
foreach num in 0 1 2 {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		& `year'_polinttri==`num'	//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)	

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		& `year'_polinttri==`num'	//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'7)/2
tab1 `year'_sum`var'_pol`num'
gen `year'_sum24`var'_pol`num' = `year'_mean`var'_pol`num'1
tab1 `year'_sum24`var'_pol`num'
gen `year'_sum56`var'_pol`num' = `year'_mean`var'_pol`num'7
tab1 `year'_sum56`var'_pol`num'

}
}
}

*Differences
foreach num in 0 1 2 {

gen a_diff_zufr_pol`num'13=a_zufr3n-a_zufr1n if a_polinttri==`num'
tab a_diff_zufr_pol`num'13

gen a_diff_zufr_pol`num'35=a_zufr5n-a_zufr3n if a_polinttri==`num'
tab a_diff_zufr_pol`num'35

gen a_diff_zufr_pol`num'57=a_zufr7n-a_zufr5n if a_polinttri==`num'
tab a_diff_zufr_pol`num'57

gen a_stab_zufr_pol`num'13 =1-abs(a_diff_zufr_pol`num'13/4) if a_zufr1n==0 | a_zufr1n==4
replace a_stab_zufr_pol`num'13 =1-abs(a_diff_zufr_pol`num'13/3) if a_zufr1n==1 | a_zufr1n==3
replace a_stab_zufr_pol`num'13 =1-abs(a_diff_zufr_pol`num'13/2) if a_zufr1n==2
tab a_stab_zufr_pol`num'13

gen a_stab_zufr_pol`num'35 =1-abs(a_diff_zufr_pol`num'35/4) if a_zufr3n==0 | a_zufr3n==4
replace a_stab_zufr_pol`num'35 =1-abs(a_diff_zufr_pol`num'35/3) if a_zufr3n==1 | a_zufr3n==3
replace a_stab_zufr_pol`num'35 =1-abs(a_diff_zufr_pol`num'35/2) if a_zufr3n==2
tab a_stab_zufr_pol`num'35

gen a_stab_zufr_pol`num'57 =1-abs(a_diff_zufr_pol`num'57/4) if a_zufr5n==0 | a_zufr5n==4
replace a_stab_zufr_pol`num'57 =1-abs(a_diff_zufr_pol`num'57/3) if a_zufr5n==1 | a_zufr5n==3
replace a_stab_zufr_pol`num'57 =1-abs(a_diff_zufr_pol`num'57/2) if a_zufr5n==2
tab a_stab_zufr_pol`num'57

}

foreach num in 0 1 2 {
foreach num2 in 13 35 57 {
	ci means a_stab_zufr_pol`num'`num2'
	gen a_stab_zufr_pol`num'`num2'_mean = r(mean)
}
}

*

*Aggregate stability
foreach year in a{
foreach num in 0 1 2 {
	foreach var in zufr {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'13_mean+`year'_stab_`var'_pol`num'35_mean+`year'_stab_`var'_pol`num'57_mean)/3
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}


*2013

*Differences
foreach num in 0 1 2 {
gen b_diff_zufr_pol`num'17=b_zufr7n-b_zufr1n  if b_polinttri==`num'
tab b_diff_zufr_pol`num'17

gen b_stab_zufr_pol`num'17 =1-abs(b_diff_zufr_pol`num'17/4) if b_zufr1n==0 | b_zufr1n==4
replace b_stab_zufr_pol`num'17 =1-abs(b_diff_zufr_pol`num'17/3) if b_zufr1n==1 | b_zufr1n==3
replace b_stab_zufr_pol`num'17 =1-abs(b_diff_zufr_pol`num'17/2) if b_zufr1n==2
tab b_stab_zufr_pol`num'17
}

foreach num in 0 1 2 {
foreach num2 in 17 {
	ci means b_stab_zufr_pol`num'`num2'
	gen b_stab_zufr_pol`num'`num2'_mean = r(mean)
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 0 1 2 {
	foreach var in zufr {
		gen `year'_stab_`var'_pol`num'_aggr=`year'_stab_`var'_pol`num'17_mean
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}








*Separate analysis for party identification


***Aggregation of wave means
estimates clear
foreach year in a {
foreach num in 0 1 {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.	& `year'_pidyes==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		& `year'_pidyes==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	& `year'_pidyes==`num'		//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		& `year'_pidyes==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'7)/4
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'3)/2
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum56`var'_pid`num'

}
}
}



***Aggregation of wave means
estimates clear
foreach year in  b {
foreach num in 0 1 {
foreach var in zufr {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		& `year'_pidyes==`num'	//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)	

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		& `year'_pidyes==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'7)/2
tab1 `year'_sum`var'_pid`num'
gen `year'_sum24`var'_pid`num' = `year'_mean`var'_pid`num'1
tab1 `year'_sum24`var'_pid`num'
gen `year'_sum56`var'_pid`num' = `year'_mean`var'_pid`num'7
tab1 `year'_sum56`var'_pid`num'

}
}
}

*Differences
foreach num in 0 1 {

gen a_diff_zufr_pid`num'13=a_zufr3n-a_zufr1n if a_pidyes==`num'
tab a_diff_zufr_pid`num'13

gen a_diff_zufr_pid`num'35=a_zufr5n-a_zufr3n if a_pidyes==`num'
tab a_diff_zufr_pid`num'35

gen a_diff_zufr_pid`num'57=a_zufr7n-a_zufr5n if a_pidyes==`num'
tab a_diff_zufr_pid`num'57

gen a_stab_zufr_pid`num'13 =1-abs(a_diff_zufr_pid`num'13/4) if a_zufr1n==0 | a_zufr1n==4
replace a_stab_zufr_pid`num'13 =1-abs(a_diff_zufr_pid`num'13/3) if a_zufr1n==1 | a_zufr1n==3
replace a_stab_zufr_pid`num'13 =1-abs(a_diff_zufr_pid`num'13/2) if a_zufr1n==2
tab a_stab_zufr_pid`num'13

gen a_stab_zufr_pid`num'35 =1-abs(a_diff_zufr_pid`num'35/4) if a_zufr3n==0 | a_zufr3n==4
replace a_stab_zufr_pid`num'35 =1-abs(a_diff_zufr_pid`num'35/3) if a_zufr3n==1 | a_zufr3n==3
replace a_stab_zufr_pid`num'35 =1-abs(a_diff_zufr_pid`num'35/2) if a_zufr3n==2
tab a_stab_zufr_pid`num'35

gen a_stab_zufr_pid`num'57 =1-abs(a_diff_zufr_pid`num'57/4) if a_zufr5n==0 | a_zufr5n==4
replace a_stab_zufr_pid`num'57 =1-abs(a_diff_zufr_pid`num'57/3) if a_zufr5n==1 | a_zufr5n==3
replace a_stab_zufr_pid`num'57 =1-abs(a_diff_zufr_pid`num'57/2) if a_zufr5n==2
tab a_stab_zufr_pid`num'57

}

foreach num in 0 1 {
foreach num2 in 13 35 57 {
	ci means a_stab_zufr_pid`num'`num2'
	gen a_stab_zufr_pid`num'`num2'_mean = r(mean)
}
}

*

*Aggregate stability
foreach year in a{
foreach num in 0 1 {
	foreach var in zufr {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'13_mean+`year'_stab_`var'_pid`num'35_mean+`year'_stab_`var'_pid`num'57_mean)/3
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}


*2013

*Differences
foreach num in 0 1 {
gen b_diff_zufr_pid`num'17=b_zufr7n-b_zufr1n  if b_pidyes==`num'
tab b_diff_zufr_pid`num'17

gen b_stab_zufr_pid`num'17 =1-abs(b_diff_zufr_pid`num'17/4) if b_zufr1n==0 | b_zufr1n==4
replace b_stab_zufr_pid`num'17 =1-abs(b_diff_zufr_pid`num'17/3) if b_zufr1n==1 | b_zufr1n==3
replace b_stab_zufr_pid`num'17 =1-abs(b_diff_zufr_pid`num'17/2) if b_zufr1n==2
tab b_stab_zufr_pid`num'17
}

foreach num in 0 1 {
foreach num2 in 17 {
	ci means b_stab_zufr_pid`num'`num2'
	gen b_stab_zufr_pid`num'`num2'_mean = r(mean)
}
}
*

*Aggregate stability
foreach year in b{
foreach num in 0 1 {
	foreach var in zufr {
		gen `year'_stab_`var'_pid`num'_aggr=`year'_stab_`var'_pid`num'17_mean
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}


********************************************************************************


***Percentage intra-individual change

foreach year in a b {
	foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

**Separate for political interest

foreach year in a b {
foreach num in 0 1 2 {
	foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_polinttri1==`num' [aw=`year'_weight], cell
}
}
}

**Separate for party identification

foreach year in a b {
foreach num in 0 1 {
	foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_pidyes1==`num' [aw=`year'_weight], cell
}
}
}


*Drop unneccessary aiding variables

foreach year in a b{
	foreach var in wkampfint extefficacy zufr entfremda intefficacy indiff {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pol???
	drop `year'_diff_`var'_pid???
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pol???
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pol???_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_`var'?n
}
}



save "${data}\data2009_2013 participants 7 waves.dta", replace

	